package gases.as3.librarys.components.ui.control
{
	import gases.as3.librarys.components.events.GButtonEvent;
	import gases.as3.librarys.components.events.GListEvent;
	import gases.as3.librarys.components.skin.SkinStylesMangaer;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.basic.GLabel;
	import gases.as3.util.SkinStyleName;
	
	public class GComboBox extends GUIComponents
	{
		public function GComboBox(defaultLabel:String = "",labelWidth:Number = 0,
								  listArray:Array = null,showScrollBar:Boolean = false,showHeight:Number = 100)
		{
			_defaultLabel = defaultLabel;
			_labelWidth = labelWidth;
			_listArray = listArray;
			_showScrollBar = showScrollBar;
			_showHeight = showHeight;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.mouseEnabled = true;
			
			_label = new GLabel(_defaultLabel,0xFF00FF,12,_labelWidth,25);
			_btn = new GButton();
			_btn.x = _labelWidth;
			_btn.selectState = true;
			_btn.isSelect = false;
			_list = new GList();
			_list.y = _label.height;
			_list.setWidth(_labelWidth);
			_list.dataProvider = _listArray;
			if(!_btn.skin)
			{
				_btn.skin = SkinStylesMangaer.instance.getSkinData(SkinStyleName.DEFAULT_COMBOBOX) as BaseSkin;
			}
			
			this.addChild(_label);
			this.addChild(_btn);
			this.addChild(_list);
			
			if(_showScrollBar)
			{
				_scrollBar = new GScrollBar(_labelWidth,_showHeight,_list);
				_scrollBar.x = _labelWidth;
				_scrollBar.y = _list.y;
				this.addChild(_scrollBar);
				
			}
			
			addListData();
			addEventListeners();
			showState();
		}
		
		public function setSkin(btnSkin:BaseSkin,scrollBarSkin:BaseSkin = null):void
		{
			_btn.skin = btnSkin;
			if(_showScrollBar)
			{
				_scrollBar.skin = scrollBarSkin;
			}
			showState();
		}
		
		/**
		 *改变List的数据 
		 * @param arr
		 * 
		 */		
		public function changeListArray(arr:Array):void
		{
			_listArray = arr;
			addListData();
			if(_showScrollBar)
			{
				_scrollBar.updateScroll();
				_scrollBar.visible = false;
			}
			showState();
		}
		
		private function addEventListeners():void
		{
			_btn.addEventListener(GButtonEvent.BUTTON_CLICK,btnClick);
			_list.addEventListener(GListEvent.ITEM_CLICK,listClick);
		}
		
		private function btnClick(e:GButtonEvent):void
		{
			_open = !_open;
			showState();
		}
		
		private function listClick(e:GListEvent):void
		{
			_label.text = String(e.data.txt);
			_selectIndex = e.index;
			_open = false;
			showState();
		}
		/**
		 *加载数据 
		 * 
		 */		
		private function addListData():void
		{
			_list.dataProvider = _listArray;
		}
		
		/**
		 *显示状态 
		 * 
		 */		
		private function showState():void
		{
			if(!_open)
			{
				if(_showScrollBar)
				{
					_scrollBar.visible = false;
					_list.visible = false;
				}
				else
				{
					_list.visible = false;
				}
			}
			else 
			{
				if(_showScrollBar)
				{
					if(_list.Gheight > _showHeight)
					{
						_scrollBar.visible = true;
					}
					_list.visible = true;
				}
				else
				{
					_list.visible = true;
				}
			}
		}
		
		
		private var _defaultLabel:String = "";
		private var _listArray:Array;
		private var _open:Boolean = false;
		private var _labelWidth:Number = 120;
		private var _showScrollBar:Boolean = false;
		private var _showHeight:Number = 100;//显示scroolbar的高度
		
		private var _selectIndex:int;
		
		private var _btn:GButton;
		private var _label:GLabel;
		private var _list:GList;
		private var _scrollBar:GScrollBar;
	}
}